<template>
  <el-card class="box-card card-main" shadow="never">
    <div slot="header">
      <el-row>
        <el-button size="mini" @click="$router.go(-1)">返回</el-button>
        <el-button size="mini" type="primary" @click="addRow()">新增用例</el-button>
      </el-row>
    </div>
    <el-table :data="dataList" border stripe size="small" style="width: auto">
      <el-table-column align="left" width="60px" label="用例ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column align="left" width="70px" label="执行顺序">
        <!-- <template slot-scope="scope">{{ scope.row.sort}}</template> -->
        <template slot-scope="scope">
          <el-popover v-model="scope.row.sortEdit" placement="right" width="180" trigger="manual">
            <el-input
              v-model="scope.row.orgSort"
              size="mini"
              style="width:72px"
              class="edit-input"
            />
            <el-button
              size="mini"
              style="padding: 3px 4px 3px 4px;margin: 1px"
              @click="editSort(scope.row,0)"
            >取消</el-button>
            <el-button
              size="mini"
              style="padding: 3px 4px 3px 4px;margin: 1px"
              type="primary"
              @click="editSort(scope.row,1)"
            >确定</el-button>
            <span
              slot="reference"
              style="cursor:pointer;color: rgb(48, 48, 247);text-decoration: underline;"
              @click="editSort(scope.row,2)"
            >{{ scope.row.sort }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="left" width="150px" label="用例名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column align="left" width="150px" label="项目名称">
        <template slot-scope="scope">{{ scope.row.projectName }}</template>
      </el-table-column>
      <el-table-column align="left" label="用例描述">
        <template slot-scope="scope">{{ scope.row.remark }}</template>
      </el-table-column>
      <el-table-column width="160" align="left" label="创建时间">
        <template slot-scope="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column width="180" fixed="right" label="操作">
        <template slot-scope="scope">
          <!-- <el-button
            style="padding: 3px 4px 3px 4px;margin: 1px"
            size="mini"
            @click="addRow(scope.row)"
          >修改</el-button>-->
          <el-button
            type="danger"
            style="padding: 3px 4px 3px 4px;margin: 1px"
            size="mini"
            @click="deleteRow(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="pageInfo.pageNum"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.pageCount"
      @size-change="changePageSize"
      @current-change="changePageNum"
    />
    <!--新增 -->
    <el-dialog
      :title="addForm.title"
      :visible.sync="addFormVisible"
      width="70%"
      @close="closeDialog('addForm')"
    >
      <el-table
        ref="table"
        :data="dataList1"
        border
        stripe
        size="small"
        style="width: auto"
        @selection-change="selected"
      >
        <el-table-column type="selection" align="left" width="35" />
        <el-table-column align="left" width="60px" label="ID">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column align="left" width="150px" label="用例名称">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column align="left" width="150px" label="项目名称">
          <template slot-scope="scope">{{ scope.row.projectName }}</template>
        </el-table-column>
        <el-table-column align="left" label="用例描述">
          <template slot-scope="scope">{{ scope.row.remark }}</template>
        </el-table-column>
        <el-table-column width="160" align="left" label="创建时间">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('addForm')">取消</el-button>
        <el-button size="small" type="primary" class="title" @click="submitForm('addForm')">保存</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { requestTestCaseByBusinessId, requestTestCasePage } from '@/api/testCase/ui'
import { requestDelBusinessCaseById, requestAddCaseToBusiness, requestUpdateCaseSort } from '@/api/testBusiness/ui'

import { successMessage } from '@/utils/message'

export default {
  data() {
    return {
      visible: false,
      businessId: this.$route.query.id,
      serchData: { name: '', projectName: '', remark: '' },
      pageInfo: { pageNum: 1, pageSize: 10, pageCount: 0 },
      pageInfo1: { pageNum: 1, pageSize: 10, pageCount: 0 },
      dataList: [],
      dataList1: [],
      selectedData: [],
      addFormVisible: false, // 新增弹出框默认值
      rules: {
        projectName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ]
      },
      addForm: {
        title: '',
        id: '',
        name: '',
        projectName: '',
        remark: ''
      }
    }
  },
  computed: {
    serchInfo: function() {
      const pageInfo = this.pageInfo
      const businessId = this.businessId
      pageInfo.businessId = businessId
      return pageInfo
    },
    serchInfo1: function() {
      const pageInfo = this.pageInfo1
      const serchData = this.serchData
      pageInfo.serchData = serchData
      return pageInfo
    },
    selectedCases: function() {
      const selectedData = this.selectedData
      const serchData = selectedData.map(v => {
        v.businessId = parseInt(this.$route.query.id)
        return v
      })
      return serchData
    }
  },
  mounted() {
    this.getPageList()
    // this.getPageList1()
  },
  methods: {
    editSort(row, type) {
      if (type === 1) {
        // 确认
        if (row.orgSort === '') {
          this.$message.error('排序不能为空')
          return
        } else if (!(/(^[0-9]\d*$)/.test(row.orgSort))) {
          this.$message.error('排序只能填写0和正整数')
          return
        }
        row.sort = row.orgSort
        requestUpdateCaseSort(row).then(data => {
          this.$message({
            type: 'success',
            message: '修改排序成功!'
          })
          this.getPageList()
        })
        row.sortEdit = false
      } else if (type === 0) {
        // 取消
        row.sortEdit = false
      } else {
        // 打开
        row.orgSort = row.sort
        row.sortEdit = !row.sortEdit
      }
    },
    changePageSize(row) {
      // console.log(row)
      this.pageInfo.pageSize = row
      this.getPageList()
    },
    selected(val) {
      this.selectedData = val
    },
    changePageNum(row) {
      this.pageInfo.pageNum = row
      this.getPageList()
    },
    addRow(row) {
      // 新增
      this.getPageList1()
      this.addFormVisible = true
      this.addForm.title = '新增用例'
    },
    deleteRow(row) {
      this.$confirm('此操作将永久删除该用例, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        requestDelBusinessCaseById(row.businessCaseId).then(data => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getPageList()
        })
      }).catch(() => {
        this.$message({
          type: 'success',
          message: '已取消删除'
        })
      })
    },
    submitForm(Visible) {
      // console.log(this.selectedCases.length)
      if (Visible === 'addForm' && this.selectedCases.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择一个用例',
          type: 'error'
        })
      } else if (Visible === 'addForm' && this.selectedCases.length > 0) {
        // 新增用例
        requestAddCaseToBusiness(this.selectedCases).then(data => {
          this.addFormVisible = false
          this.getPageList()
          successMessage('保存数据成功')
        })
      }
    },
    closeDialog(Visible) {
      if (Visible === 'addForm') {
        // this.$refs[Visible].resetFields()
        this.addFormVisible = false
        this.resetDialog()
        this.$refs.table.clearSelection()
      }
    },
    // 获取分页用例列表
    getPageList() {
      requestTestCaseByBusinessId(this.serchInfo).then((result) => {
        this.dataList = result.list.map(v => {
          v.sortEdit = false
          v.orgSort = v.sort
          return v
        }) || []
        // this.dataList = result.list
        this.pageInfo.pageCount = result.total
      })
    },

    // 获取分页用例列表
    getPageList1() {
      requestTestCasePage(this.serchInfo1).then((result) => {
        this.dataList1 = result.list || []
        this.pageInfo1.pageCount = result.total
      })
    },
    resetDialog() {
      this.addForm.id = ''
      this.addForm.name = ''
      this.addForm.projectName = ''
      this.addForm.remark = ''
    }
  }
}
</script>

<style scoped>
</style>
